<template>
	<view>
		<input v-if="userinfo.password" class="uni-input" type="text" v-model="oldpassword" placeholder="输入旧密码" />
		<input class="uni-input" type="text" v-model="newpassword" placeholder="输入新密码" />
		<input class="uni-input" type="text" v-model="renewpassword" placeholder="输入确认密码" />
		<view class="pt-4 px-2">
			<button :class="isDisable ? 'bg-main-disabled':'bg-main'" class=" text-white  text-center" 
			style="border-radius: 50rpx;" :disabled="isDisable"
			@click="submit">
				完成
			</button>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default {
		data() {
			return {
				oldpassword:' ',
				newpassword:'',
				renewpassword:''
			}
		},
		computed:{
			isDisable(){
				if(this.userinfo.password){
					if(this.oldpassword === '' || this.newpassword === '' || this.renewpassword === ''){
						return true
					}
					return false
				}else{
					if(this.newpassword === '' || this.renewpassword === ''){
						return true
					}
					return false
				}
				
			},
			...mapState({
				userinfo:state =>state.userinfo
			})
		},
		methods: {
			check(){
				if(this.newpassword !== this.renewpassword){
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none'
					});
					return false
				}
				return true
			},
			submit(){
				if(!this.check()){
					return
				}
				this.$H.post('/repassword',{
					oldpassword:this.oldpassword,
					newpassword:this.newpassword,
					renewpassword:this.renewpassword
				},{
					token:true
				}).then( res => {
					console.log(res)
					this.$store.commit('editUserInfo',{
						key:'password',
						value:true
					})
					uni.navigateBack({
						delta: 1
					});
					uni.showToast({
						title: '修改成功',
						icon: 'none'
					});
				})				
			}			
		}
	}
</script>

<style>

</style>
